from django import forms
from django.core.exceptions import ValidationError
from django.core.validators import RegexValidator

from .utils.encrypt import md5
from .utils.bootstrap import BootStrapForm, BootStrapModelForm
from . import models

class LoginModelForm(BootStrapModelForm):
    code = forms.CharField(
        label="验证码",
        widget=forms.TextInput,
        required=True
    )

    class Meta:
        model = models.Admin
        fields = ["username", "password", "code"]
        widgets = {
            "username": forms.TextInput(),
            "password": forms.PasswordInput(render_value=True)
        }

    def clean_password(self):
        pwd = self.cleaned_data.get("password")
        return md5(pwd)


class AdminModelForm(BootStrapModelForm):
    confirm_password = forms.CharField(
        label="确认密码",
        widget=forms.PasswordInput(render_value=True)
    )

    class Meta:
        model = models.Admin
        fields = ["username", "password", "confirm_password"]
        widgets = {
            "password": forms.PasswordInput(render_value=True)
        }


    def clean_password(self):
        pwd = self.cleaned_data.get("password")
        return md5(pwd)


    def clean_confirm_password(self):
        # 返回加密的密码
        # 已经是加完密的密码
        pwd = self.cleaned_data.get("password")
        confirm = md5(self.cleaned_data.get("confirm_password"))
        if confirm != pwd:
            raise ValidationError("密码不一致")
        return confirm


class AdminEditModelForm(BootStrapModelForm):
    class Meta:
        model = models.Admin
        fields = ["username"]


class AdminResetModelForm(BootStrapModelForm):
    confirm_password = forms.CharField(
        label="确认密码",
        widget=forms.PasswordInput(render_value=True)
    )

    class Meta:
        model = models.Admin
        fields = ["password", "confirm_password"]
        widgets = {
            "password": forms.PasswordInput(render_value=True)
        }

    def clean_password(self):
        pwd = self.cleaned_data.get("password")
        md5_pwd = md5(pwd)

        # 去数据库效验当前密码和新输入的密码是否一致
        exists = models.Admin.objects.filter(id=self.instance.pk, password=md5_pwd).exists()
        if exists:
            raise ValidationError("不能与以前的密码相同")

        return md5_pwd

    def clean_confirm_password(self):
        # 返回加密的密码
        # 已经是加完密的密码
        pwd = self.cleaned_data.get("password")
        confirm = md5(self.cleaned_data.get("confirm_password"))
        if confirm != pwd:
            raise ValidationError("密码不一致")
        return confirm


class UserMyForm(BootStrapModelForm):
    name = forms.CharField(min_length=3, label="用户名")

    class Meta:
        model = models.EmployInfo
        fields = ["name", "password", "age", "account", "create_time", "gender", "depart"]


class PrettyModelForm(BootStrapModelForm):
    # 验证方法1
    mobile = forms.CharField(
        label="手机号",
        validators=[RegexValidator(r'^1[3-9]\d{9}$', "手机号格式错误")]
    )

    class Meta:
        model = models.PrettyNum
        # fields = "__all__"
        # exclude["level]
        fields = ["mobile", "price", "level", "status"]


    # 验证方法2
    def clean_mobile(self):
        # 获取输入的数据
        txt_mobile = self.cleaned_data["mobile"]
        exists = models.PrettyNum.objects.filter(mobile=txt_mobile).exists()
        if exists:
            raise ValidationError("手机号已存在")

        # 验证通过 用户返回输入值
        return txt_mobile

    #     # 验证不通过
    #     if len(txt_mobile) != 11:
    #         raise ValidationError("格式错误")
    #     # 验证通过 用户返回输入值
    #     return txt_mobile


class PrettyEditModelForm(BootStrapModelForm):
    # mobile = forms.CharField(disabled=True, label="手机号")
    mobile = forms.CharField(
        label="手机号",
        validators=[RegexValidator(r'^1[3-9]\d{9}$', "手机号格式错误")]
    )

    class Meta:
        model = models.PrettyNum
        fields = ["mobile", "price", "level", "status"]


    def clean_mobile(self):
        # 当前编辑那一行的ID
        # print(self.instance.pk)

        # 获取输入的数据
        txt_mobile = self.cleaned_data["mobile"]
        exists = models.PrettyNum.objects.exclude(id=self.instance.pk).filter(mobile=txt_mobile).exists()
        if exists:
            raise ValidationError("手机号已存在")

        # 验证通过 用户返回输入值
        return txt_mobile

    def clean_price(self):
        # 当前编辑那一行的ID
        # print(self.instance.pk)

        # 获取输入的数据
        txt_price = self.cleaned_data["price"]

        # 验证通过 用户返回输入值
        return txt_price

    def clean(self):
        level = self.cleaned_data["level"]
        price = self.cleaned_data["price"]
        exists = models.PrettyNum.objects.filter(level=level, price=price).exists()
        if exists:
            raise ValidationError("价格和等级不能同时一样")

        return self.cleaned_data


class TaskModelForm(BootStrapModelForm):
    class Meta:
        model = models.Task
        fields = "__all__"
        widgets = {
            "detail": forms.TextInput
        }


class OrderModelForm(BootStrapModelForm):
    class Meta:
        model = models.Order
        fields = "__all__"
        exclude = ["oid", "admin"]


class UpForm(BootStrapForm):
    bootstrap_exclude_fields = ["img"]

    name = forms.CharField(label="姓名")
    age = forms.IntegerField(label="年龄")
    img = forms.FileField(label="头像")


class UpModelForm(BootStrapModelForm):
    bootstrap_exclude_fields = ["img"]
    class Meta:
        model = models.City
        fields = "__all__"



